package com.zxw.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zxw.common.core.base.entity.BaseEntity;
import com.zxw.common.web.annotation.InitUpdateInfo;
import com.zxw.common.web.util.JwtUtils;
import com.zxw.user.domain.dto.DeptDTO;
import com.zxw.user.domain.dto.DeptSelectDTO;
import com.zxw.user.domain.entity.Dept;
import com.zxw.user.mapper.DeptMapper;
import com.zxw.user.service.DeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Xuewei.Zhang
 * @since 2022-03-10
 */
@Service
@RequiredArgsConstructor
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {

    private final DeptMapper deptMapper;

    @Override
    public List<DeptDTO> getAllByCompanyIdList(Dept dept) {
        Long companyId = JwtUtils.getCompanyId();
        dept.setCompanyId(companyId);
        List<Dept> depts = deptMapper.getAllByCompanyIdList(dept);
        return recursiveGenreateDept(depts, 0l);
    }

    @Override
    public List<DeptSelectDTO> getDeptSelectList() {
        Long companyId = JwtUtils.getCompanyId();
        List<Dept> depts = deptMapper.getIdAndNameByCompanyId(companyId);
        return recursiveGenreateSelectDept(depts, 0l);
    }

    @Override
    public Dept add(Dept dept) {
        BaseEntity baseEntity = JwtUtils.getBaseEntity();
        BeanUtil.copyProperties(baseEntity, dept);
        if (dept.getParentId() > 0) {
            Dept pdept = deptMapper.selectById(dept.getParentId());
            dept.setPathNumber("-" + pdept.getParentId() + "-" + pdept.getId() + "-");
        } else {
            dept.setPathNumber("-0-");
        }
        deptMapper.insert(dept);
        return dept;
    }


    @Override
    public void delete(String ids) {
        QueryWrapper<Dept> wrapper = new QueryWrapper<>();
        wrapper.in("id", ids);
        deptMapper.delete(wrapper);
    }

    @Override
    @InitUpdateInfo
    public void update(Dept dept) {
        deptMapper.updateById(dept);
    }


    /**
     * 递归组装部门树结构数据
     *
     * @param depts
     * @param parentId
     * @return
     */
    public List<DeptSelectDTO> recursiveGenreateSelectDept(List<Dept> depts, Long parentId) {
        List<DeptSelectDTO> deptDTOList = new ArrayList<>();
        Optional.ofNullable(depts).ifPresent(deptList -> deptList.stream().filter(dept -> dept.getParentId().equals(parentId))
                .forEach(dept -> {
                    DeptSelectDTO deptDTO = new DeptSelectDTO(dept.getId(), dept.getName());
                    List<DeptSelectDTO> childrenDeptDTOList = recursiveGenreateSelectDept(depts, deptDTO.getId());
                    deptDTO.setChildren(childrenDeptDTOList);
                    deptDTOList.add(deptDTO);
                })
        );
        return deptDTOList;
    }

    /**
     * 递归组装部门树结构数据
     *
     * @param depts
     * @param parentId
     * @return
     */
    public List<DeptDTO> recursiveGenreateDept(List<Dept> depts, Long parentId) {
        List<DeptDTO> deptDTOList = new ArrayList<>();
        Optional.ofNullable(depts).ifPresent(deptList -> deptList.stream().filter(dept -> dept.getParentId().equals(parentId))
                .forEach(dept -> {
                    DeptDTO deptDTO = new DeptDTO();
                    BeanUtil.copyProperties(dept, deptDTO);
                    List<DeptDTO> childrenDeptDTOList = recursiveGenreateDept(depts, deptDTO.getId());
                    deptDTO.setChildren(childrenDeptDTOList);
                    deptDTOList.add(deptDTO);
                })
        );
        return deptDTOList;
    }

}
